<%@ page contentType="text/html;charset=UTF-8" %>
    <script>
    $(document).ready(function() {
        $('#leaveDialogTable').bootstrapTable({

            //请求方法
            method: 'get',
            //类型json
            dataType: "json",
            //显示刷新按钮
            showRefresh: true,
            //显示切换手机试图按钮
            showToggle: true,
            //显示 内容列下拉框
            showColumns: true,
            //显示到处按钮
            showExport: true,
            //显示切换分页按钮
            showPaginationSwitch: true,
            //最低显示2行
            minimumCountColumns: 2,
            //是否显示行间隔色
            striped: true,
            //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            cache: false,
            //是否显示分页（*）
            pagination: true,
            //排序方式
            sortOrder: "asc",
            //初始化加载第一页，默认第一页
            pageNumber:1,
            //每页的记录行数（*）
            pageSize: 16,
            //可供选择的每页的行数（*）
            pageList: [16, 30, 50, 100],
            //这个接口需要处理bootstrap table传递的固定参数,并返回特定格式的json数据
            url: "${ctx}/sys/OrderCollection/data",
            //默认值为 'limit',传给服务端的参数为：limit, offset, search, sort, order Else
            //queryParamsType:'',
            ////查询参数,每次调用是会带上这个参数，可自定义
            queryParams : function(params) {
                var searchParam = $("#searchForm").serializeJSON();
                searchParam.pageNo = params.limit === undefined? "1" :params.offset/params.limit+1;
                searchParam.pageSize = params.limit === undefined? -1 : params.limit;
                searchParam.orderBy = params.sort === undefined? "" : params.sort+ " "+  params.order;
                return searchParam;
            },
            //分页方式：client客户端分页，server服务端分页（*）
            sidePagination: "server",
            contextMenuTrigger:"right",//pc端 按右键弹出菜单
            contextMenuTriggerMobile:"press",//手机端 弹出菜单，click：单击， press：长按。
            contextMenu: '#context-menu',
            onContextMenuItem: function(row, $el){
                if($el.data("item") == "edit"){
                    edit(row.id);
                } else if($el.data("item") == "delete"){
                    jp.confirm('确认要删除该表单记录吗？', function(){
                        // jp.loading();
                        // jp.get("${ctx}/test/one/leaveDialog/delete?id="+row.id, function(data){
                        //     if(data.success){
                        //         $('#leaveDialogTable').bootstrapTable('refresh');
                        //         jp.success(data.msg);
                        //     }else{
                        //         jp.error(data.msg);
                        //     }
                        // })

                    });

                }
            },

            onClickRow: function(row, $el){
            },
            columns: [{
                checkbox: true

            }
                ,
                {
                    field: 'ID',
                    title: 'ID',
                    sortable: true,
                    visible: false
                }
                ,{
                    field: 'ORD_SEQ',
                    title: '序号',
                    sortable: true

                }
                ,{
                    field: 'CX_ITEM',
                    title: '畅想货号',
                    sortable: true

                }
                ,{
                    field: 'ERP_ITEM',
                    title: 'ERP货号',
                    sortable: true

                }
                ,{
                    field: 'ITEM_DESCR',
                    title: '商品描述',
                    sortable: true

                }
                ,{
                    field: 'SUPPL_NAME',
                    title: '供应商名',
                    sortable: true

                }
                ,{
                    field: 'SPCI',
                    title: '规格',
                    sortable: true

                }
                ,{
                    field: 'MIN_ORD_QTY',
                    title: '最小订货数量',
                    sortable: true

                }
                ,{
                    field: 'UNIT',
                    title: '单位',
                    sortable: true

                }
                ,{
                    field: 'ORD_QTY',
                    title: '订货数量',
                    sortable: true

                }
                ,{
                    field: 'CREATE_DATE',
                    title: '导入日期',
                    sortable: true

                }
            ]

        });


        if(navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)){//如果是移动端


            $('#leaveDialogTable').bootstrapTable("toggleView");
        }

        $('#leaveDialogTable').on('check.bs.table uncheck.bs.table load-success.bs.table ' +
            'check-all.bs.table uncheck-all.bs.table', function () {
            $('#remove').prop('disabled', ! $('#leaveDialogTable').bootstrapTable('getSelections').length);
            $('#edit').prop('disabled', $('#leaveDialogTable').bootstrapTable('getSelections').length!=1);
        });
        $('#btnDownImpTemp').click(function () {
            window.location='${ctx}/sys/OrderCollection/import/template';
        })
        $("#btnImport").click(function(){
            jp.open({
                type: 1,
                area: [500, 300],
                title:"导入数据",
                content:$("#importBox").html() ,
                btn: ['下载模板','确定', '关闭'],
                btn1: function(index, layero){
                    window.location='${ctx}/sys/OrderCollection/import/template';
                },
                btn2: function(index, layero){
                    var inputForm =top.$("#importForm");
                    var top_iframe = top.getActiveTab().attr("name");//获取当前active的tab的iframe
                    inputForm.attr("target",top_iframe);//表单提交成功后，从服务器返回的url在当前tab中展示
                    inputForm.onsubmit = function(){
                        jp.loading('  正在导入，请稍等...');
                    }
                    inputForm.submit();
                    jp.close(index);
                },

                btn3: function(index){
                    jp.close(index);
                }
            });
        });

        $("#search").click("click", function() {// 绑定查询按扭
            $('#leaveDialogTable').bootstrapTable('refresh');
        });

        $("#reset").click("click", function() {// 绑定查询按扭
            $("#searchForm  input").val("");
            $("#searchForm  select").val("");
            $("#searchForm  .select-item").html("");
            $('#leaveDialogTable').bootstrapTable('refresh');
        });

        $('#beginBeginDate').datetimepicker({
            format: "YYYY-MM-DD HH:mm:ss"
        });
        $('#endBeginDate').datetimepicker({
            format: "YYYY-MM-DD HH:mm:ss"
        });
        $('#endDate').datetimepicker({
            format: "YYYY-MM-DD HH:mm:ss"
        });
        $('#btnCommit').click(function () {
            var loading;
            $.ajax({
                url:'${ctx}/sys/OrderCollection/orderCommit',
                dataType:'json',
                type:'get',
                beforeSend:function () {

                    loading=jp.loading('正在提交中...')
                },
                success:function (data) {
                    jp.close(loading)
                    console.log(data)
                    if(data.success==true)
                    {
                        jp.alert(data.msg)
                    }
                    else
                    {
                        jp.alert(data.msg)
                    }
                },error:function () {
                    jp.close(loading)
                    jp.alert('操作异常')
                }
            })
        })
        $('#btnSelectTime').click(function () {
            jp.openDialog("ordTime","修改订单时间","${ctx}/sys/OrderCollection/datetime",'40%','70%',function (value) {
                if(value == null || value == undefined || value==''){
                   jp.alert("请选择时间!");
                   return;
                }
                var loading=jp.loading("正在修改订单时间...")
                jp.get('${ctx}/sys/OrderCollection/updateTime?selectTime='+value,function (data) {
                    jp.close(loading)
                    if(data.success==true)
                    {
                        jp.success(data.msg);
                    }
                    else
                    {
                        jp.error(data.msg);
                    }
                })
                jp.close(loading)
            });
        })
    });

function getIdSelections() {
    return $.map($("#leaveDialogTable").bootstrapTable('getSelections'), function (row) {
        return row.id
    });
}

function deleteAll(){

    jp.confirm('确认要删除该表单记录吗？', function(){
        //jp.loading();
        var row=$("#leaveDialogTable").bootstrapTable('getSelections');
        if(row.length<=0){
            jp.alert('请选择要删除的数据!');
            return;
        }
        var data=[];
        $.each(row,function (i,v) {
            data.push({
                id:v.ID,
                erpItem:v.ERP_ITEM,
                ordSeq:v.ORD_SEQ
            })
        })
        var loading;
        $.ajax({
            url:"${ctx}/sys/OrderCollection/delete",
            data:{ids:encodeURI(JSON.stringify(data))},
            dataType:'json',
            type:'POST',
            beforeSend:function () {

                loading=jp.loading('正在提交中...')
            },
            success:function (data) {
                jp.close(loading)
                if(data.success==true)
                {
                    jp.success(data.msg);
                    $('#leaveDialogTable').bootstrapTable('refresh');
                }
                else{
                    jp.error(data.msg);
                }
            }
            ,error:function () {
                jp.close(loading)
                jp.error("操作异常")
            }
        })
    })
}
function add(){
    jp.openDialog('新增请假表单', "${ctx}/test/one/leaveDialog/form",'800px', '500px', $('#leaveDialogTable'));
}
function edit(id){//没有权限时，不显示确定按钮
    if(id == undefined){
        id = getIdSelections();
    }
<shiro:hasPermission name="test:one:leaveDialog:edit">
        jp.openDialog('编辑请假表单', "${ctx}/test/one/leaveDialog/form?id=" + id,'800px', '500px', $('#leaveDialogTable'));
</shiro:hasPermission>
    <shiro:lacksPermission name="test:one:leaveDialog:edit">
        jp.openDialogView('查看请假表单', "${ctx}/test/one/leaveDialog/form?id=" + id,'800px', '500px', $('#leaveDialogTable'));
</shiro:lacksPermission>
}

</script>